//
//  XJFeedbackViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/7.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJFeedbackViewController.h"

@interface XJFeedbackViewController ()<UITextViewDelegate>
@property(nonatomic, strong)UITextView *textView;
/**1.灰色的线条*/
@property(nonatomic, strong) UIView *grayLine;
@property(nonatomic, strong)UIButton *submitBtn;

/**用一个labe来加装占位图， 控制alpha来显示、隐藏*/
@property(nonatomic, strong) UILabel *placeholdLb;
@end

@implementation XJFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"意见反馈";
    
    [self createSubView];
}

- (void)createSubView{
    
    self.textView = [UITextView new];
    self.textView.font = [UIFont systemFontOfSize:16.0];
    self.textView.delegate = self;
    
    [self.view addSubview:self.textView];
    
    self.placeholdLb = [UILabel new];
    self.placeholdLb.textColor = XJCOLOR_RGBA(111, 111, 111, 0.5);
    self.placeholdLb.text = @"请您输入对我们的意见或建议";
    [self.textView addSubview:self.placeholdLb];
    
    
    self.grayLine = [UIView new];
    self.grayLine.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.grayLine];
    
    self.submitBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.submitBtn.backgroundColor = XJCOLOR_RGB(255, 218, 67);
    [self.submitBtn setTitle:@"提交" forState:(UIControlStateNormal)];
    [self.submitBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.submitBtn setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
    [self.submitBtn addTarget:self action:@selector(submitAction:) forControlEvents:(UIControlEventTouchUpInside)];

    self.submitBtn.layer.masksToBounds = YES;
    self.submitBtn.layer.cornerRadius = 5;
    
    [self.view addSubview:self.submitBtn];
    
    //Masonry 设置约束
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.top.equalTo(self.view.mas_top);
        make.height.mas_equalTo(ADAPTHeight(200));
    }];
    // 假 palceholder
    [self.placeholdLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.textView.mas_left);
        make.right.equalTo(self.textView.mas_right);
        make.top.equalTo(self.textView.mas_top);
        make.height.mas_equalTo(20);
    }];
    
    [self.grayLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textView.mas_bottom);
        make.left.equalTo(self.textView.mas_left);
        make.right.equalTo(self.textView.mas_right);
        make.height.mas_equalTo(1);
    }];
    
    [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.grayLine.mas_bottom).offset(15);
        make.left.equalTo(self.textView.mas_left);
        make.right.equalTo(self.textView.mas_right);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark - submitAction
- (void)submitAction:(UIButton *)btn {
    
    NSString *str = self.textView.text;
    
    if (![self isEmptyStr:str]) {
        
        // <40 个字
        if (str.length > 40) {
            [JRToast showWithText:@"字数限制在40字符以内"];
        } else {
            [self feedbackSuccess];
        }
    }else {
        [JRToast showWithText:@"请输入内容"];
    }
}

- (void)feedbackSuccess {
    [JRToast showWithText:@"反馈成功"];
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([textView.text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    self.placeholdLb.hidden = YES;
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    // 什么没写 仍然显示占位文字
    if (!self.textView.text.length) {
        self.placeholdLb.hidden = NO;
    }
    return YES;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textView resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private
/**6-20 判断字符串是否为空*/
- (BOOL)isEmptyStr:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES; // 空
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        return YES; // 空
    }
    
    // 去掉空格和换行
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (string.length == 0) {
        return YES; // 空
    }
    return NO; // 非空
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
